import Util from '@/utils/Util';
import GoodsInfo from '@/model/GoodsInfo';
import { PayResult, Uri } from '@/constant/Constants';
import Http from '@/utils/Http';
import CreateOrderRequest, { PayOrderRequest } from '@/protocol/request/CreateOrderRequest';
import { UserService } from './UserService';
import PaymentCreateResponse from '@/protocol/response/PayCreateResponse';
import ProtocolResponse from '@/protocol/response/ProtocolResponse';
import LoggerUtil from '@/utils/LoggerUtil';
import MerchantOrder from '@/model/MerchantOrder';
import Pagination from '@/protocol/response/Pagination';
import UniUtil from '@/utils/UniUtil';
import OrderInfo from '@/model/OrderInfo';
import OrderRequest from '@/protocol/request/OrderRequest';

export namespace OrderService {
    export  function queryList(request: OrderRequest) {
        return  Http.request(Uri.user_order_query, request, new Array<OrderInfo>());
    }

    export  function queryMerchantList(request: OrderRequest) {
        return  Http.request(Uri.merchant_order_query, request, new Array<OrderInfo>());
    }

    export async function createOrder(order: OrderInfo, success?: Function, cancel?: Function, complete?: Function) {
        let request = new CreateOrderRequest();
        request.goodsList = order.goods;
        request.deliTime = order.deliTime;
        let user = UserService.getLoginResponse()

        if (!Util.isEmpty(user)) {
            request.openid = user.openId;
        }
        let response = await Http.request(Uri.user_order_create, request, new PaymentCreateResponse());
        if (null != response && !Util.isEmpty(response.getPayData())) {
            let result = await UniUtil.payment(response.getPayData());
            if (Util.isEmpty(result) && typeof (success)) {
                success();
            }
            if (null != cancel && PayResult.cancel == result && typeof (cancel)) {
                cancel();
            }

            if (complete != null && typeof (complete)) {
                complete();
            }
        } else if (null != response) {
            success();
        }
    }

    export async function payOrder(order: OrderInfo, success?: Function, cancel?: Function, complete?: Function) {
        let request = new PayOrderRequest();
        let user = UserService.getLoginResponse()
        request.orderId = order.id;
        if (!Util.isEmpty(user)) {
            request.openid = user.openId;
        }
        let response = await Http.request(Uri.user_order_pay, request, new PaymentCreateResponse());
        if (null != response) {
            let result = await UniUtil.payment(response.getPayData());
            if (Util.isEmpty(result) && typeof (success)) {
                success();
            }
            if (PayResult.cancel == result && typeof (cancel)) {
                cancel();
            }

            if (typeof (complete)) {
                complete();
            }
        }
    }

    export async function refund(orderId: number, success?: Function, cancel?: Function, complete?: Function) {
        let request = new PayOrderRequest();
        let user = UserService.getLoginResponse()
        request.orderId = orderId;
        if (!Util.isEmpty(user)) {
            request.openid = user.openId;
        }
        let response = await Http.request(Uri.merchant_order_refund, request, new ProtocolResponse());
        if (null != response) {
            success();
        }
    }

    export async function receive(orderId: number, success?: Function, cancel?: Function, complete?: Function) {
        let request = new PayOrderRequest();
        let user = UserService.getLoginResponse()
        request.orderId = orderId;
        if (!Util.isEmpty(user)) {
            request.openid = user.openId;
        }
        let response = await Http.request(Uri.merchant_order_receive, request, new ProtocolResponse());
        if (null != response) {
            success();
        }
    }
}